import { ref } from "vue"
//导入http
import { Http } from "../http/users"
import router from "../router"
import { ElMessage } from "element-plus"
//导入store
import { useUserStore } from "../stores/users"
const store = useUserStore()
export const useUserMsg = () => {
  const username = ref(store.user.username)
  const type = ref(store.user.type)
  //当用户点击注销时,清除token,返回首页
  const handleExit = async () => {
    let data = await Http.deleteUsers(store.user.id)
    if (data.code == 200) {
      store.changeToken("")
      ElMessage.success("注销用户成功")
      router.push("/")
    } else {
      ElMessage.success("注销用户失败")
    }
  }
  //用户点击退出登录,清除token,返回首页
  const handleExitLogin = async () => {
    let data = await Http.underLine(store.user.id)
    if (data.code == 200) {
      store.changeToken("")
      ElMessage.success("退出登录成功")
      router.push("/")
    } else {
      ElMessage.success("退出登录失败")
    }
  }
  return { username, type, handleExit, handleExitLogin }
}
